package com.itany.nmms.service.proxy;

import com.github.pagehelper.PageInfo;
import com.itany.nmms.entity.ProductType;
import com.itany.nmms.exception.ProductTypeExistException;
import com.itany.nmms.exception.RequestParameterErrorException;
import com.itany.nmms.exception.ServiceException;
import com.itany.nmms.factory.ObjectFactory;
import com.itany.nmms.service.ProductTypeService;
import com.itany.nmms.tran.TransactionManager;

import java.util.List;

/**
 * @author 石小俊
 * @date 2023年11月09日 9:07
 */
public class ProductTypeServiceProxy implements ProductTypeService {
    @Override
    public void addType(String name) throws RequestParameterErrorException, ProductTypeExistException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ProductTypeService typeService = (ProductTypeService) ObjectFactory.getObject("typeServiceTarget");
        try {
            tran.begin();
            typeService.addType(name);
            tran.commit();
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        } catch (ProductTypeExistException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public PageInfo<ProductType> findPage(String pageNo, String pageSize) {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ProductTypeService typeService = (ProductTypeService) ObjectFactory.getObject("typeServiceTarget");
        try {
            tran.begin();
            PageInfo<ProductType> typePage = typeService.findPage(pageNo, pageSize);
            tran.commit();
            return typePage;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public ProductType findById(String id) throws RequestParameterErrorException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ProductTypeService typeService = (ProductTypeService) ObjectFactory.getObject("typeServiceTarget");
        try {
            tran.begin();
            ProductType type = typeService.findById(id);
            tran.commit();
            return type;
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void modifyName(String id, String name) throws RequestParameterErrorException, ProductTypeExistException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ProductTypeService typeService = (ProductTypeService) ObjectFactory.getObject("typeServiceTarget");
        try {
            tran.begin();
            typeService.modifyName(id, name);
            tran.commit();
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        } catch (ProductTypeExistException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void modifyStatus(String id) throws RequestParameterErrorException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ProductTypeService typeService = (ProductTypeService) ObjectFactory.getObject("typeServiceTarget");
        try {
            tran.begin();
            typeService.modifyStatus(id);
            tran.commit();
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public List<ProductType> findEnable() {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        ProductTypeService typeService = (ProductTypeService) ObjectFactory.getObject("typeServiceTarget");
        tran.begin();
        List<ProductType> types = typeService.findEnable();
        tran.commit();
        return types;
    }
}
